#基类Base
#显示等待：
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from datetime import datetime       #导入时间
from selenium.webdriver.common.keys import Keys #键盘事件keys

#定义一个基类Base
class Base():
    #初始化方法
    def __init__(self,driver):
        self.driver = driver
    #浏览器方法
    def open_url(self,url):
        self.driver.get(url)
    #元素定位方法
    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20,0.5).until(
                lambda driver: self.driver.find_element(*loc).is_displayed
            )
            return self.driver.find_element(*loc)
        except Exception as e:
            # time = datetime.now().strftime('%Y%m%d%H%M%S')
            # a= time.screenshoot(f'phone_{time}')
            print(e)
    #点击事件方法
    def click(self,loc):
        self.find_element(loc).click()
    #输入事件方法
    def send_keys(self,loc,info):
        self.find_element(loc).send_keys(info)
    #文本方法
    def text(self,loc):
        return self.find_element(loc).text
    #清空事件方法
    def clear(self,loc):
        self.find_element(loc).clear()
    #回车事件方法
    def Enter(self,loc):
        self.find_element(loc).Enter(Keys.ENTER)
    #截屏方法
    def screenshoot(self,jiuxian):
        self.driver.save_screenshot('jiuxian.png')

    #滚动事件方法——到任意地方
    def scroll_x(self,loc,x):
        self.driver.execute_script(f'window.scrollTo(0,{x})')

    #滚动事件——滚动到底部
    def scroll_to_Height(self):
        self.driver.execute_script('window.scroll(0,document.body.scrollHeight)')
